package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"io"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"os"
)

func main() {
	r := gin.Default()

	r.GET("/trigger/upload", func(c *gin.Context) {
		resp, err := uploadFile()
		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{
				"code":    500,
				"message": err,
			})
			return
		}

		c.JSON(http.StatusOK, resp)
	})

	r.GET("/trigger/download", func(c *gin.Context) {
		err := downloadFile()
		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{
				"code":    500,
				"message": err,
			})
			return
		}

		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "success",
		})
	})

	r.Run(":18080")
}

// uploadFile 调用 client 接口上传两个文件
func uploadFile() (gin.H, error) {
	url := "http://localhost:8080/upload"

	// 文件1
	file, err := os.Open("example.txt") // Replace with the path to your file
	if err != nil {
		fmt.Printf("Error opening file: %s\n", err)
		return nil, err
	}
	defer file.Close()

	// 文件2
	file2, err := os.Open("example2.txt") // Replace with the path to your file
	if err != nil {
		fmt.Printf("Error opening file: %s\n", err)
		return nil, err
	}
	defer file2.Close()

	body := &bytes.Buffer{}
	writer := multipart.NewWriter(body)

	part, err := writer.CreateFormFile("file1", "example.txt")
	if err != nil {
		fmt.Printf("Error creating form file: %s\n", err)
		return nil, err
	}
	_, err = io.Copy(part, file)
	if err != nil {
		fmt.Printf("Error copying file content: %s\n", err)
		return nil, err
	}
	part, err = writer.CreateFormFile("file2", "example2.txt")
	if err != nil {
		fmt.Printf("Error creating form file: %s\n", err)
		return nil, err
	}
	_, err = io.Copy(part, file2)
	if err != nil {
		fmt.Printf("Error copying file content: %s\n", err)
		return nil, err
	}
	writer.Close()

	req, err := http.NewRequest("POST", url, body)
	if err != nil {
		fmt.Printf("Error creating request: %s\n", err)
		return nil, err
	}
	req.Header.Set("Content-Type", writer.FormDataContentType())

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Printf("Error sending request: %s\n", err)
		return nil, err
	}
	defer resp.Body.Close()

	responseBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Printf("Error reading response body: %s\n", err)
		return nil, err
	}

	fmt.Printf("Upload Response: %s\n", string(responseBody))
	respJson := make(gin.H)
	err = json.Unmarshal(responseBody, &respJson)
	if err != nil {
		fmt.Printf("Unmarshal response body fail: %s\n", err)
		return nil, err
	}
	return respJson, nil
}

func downloadFile() error {
	url := "http://localhost:8080/download"

	resp, err := http.Get(url)
	if err != nil {
		fmt.Printf("Error sending request: %s\n", err)
		return err
	}
	defer resp.Body.Close()

	file, err := os.Create("downloaded_file.txt") // Replace with the desired download location
	if err != nil {
		fmt.Printf("Error creating file: %s\n", err)
		return err
	}
	defer file.Close()

	_, err = io.Copy(file, resp.Body)
	if err != nil {
		fmt.Printf("Error copying response body: %s\n", err)
		return err
	}

	fmt.Println("Download successful")
	return nil
}
